vimfandomcom-20200223-history
Preview current HTML file
This tip shows how to use Vim to view an html file, or a web page, in a web browser. If wanted, a text-based web browser can be used to read the text from an html page into a scratch buffer in Vim. Viewing text from an html file or a URL A text-based web browser such as elinks can extract a formatted view of the text from an html file or a web page. The following shows how to use elinks to read that text into a scratch buffer. You might do that for a quick preview, or to copy text from the displayed html page. function! ViewHtmlText(url) if !empty(a:url) new setlocal buftype=nofile bufhidden=hide noswapfile execute 'r !elinks ' . a:url . ' -dump -dump-width ' . winwidth(0) 1d endif endfunction " Save and view text for current html file. nnoremap H :updatecall ViewHtmlText(expand('%:p')) " View text for visually selected url. vnoremap h y:call ViewHtmlText(@@) " View text for URL from clipboard. " On Linux, use @* for current selection or @+ for text in clipboard. nnoremap h :call ViewHtmlText(@+) After sourcing the above, and assuming the default backslash Leader key, you can: *Edit an html file, then type \H to save and preview the file. *Visually select the full path of a local html file or a URL, then type \h to preview the file or web page. *Copy the full path of a local html file or a URL in another application, then type \h to preview the file or web page in Vim. From 256 Opening current Vim file in your Windows browser *''TODO'': Finish cleaning following notes. On Windows, using to open the link at the cursor in a browser does not work when the URL is in an html tag surrounded by quotes. However, using works for both situations: plain URL and URL between html tags. " On Windows, open URL under cursor. nnoremap :!start C:\progra~1\intern~1\iexplore.exe nnoremap :!start C:\progra~1\mozill~1\firefox.exe " On Windows, open current file in Internet Explorer. nnoremap :updatesilent !start c:\progra~1\intern~1\iexplore.exe file://%:p " On Linux, open URL under cursor in Firefox. nnoremap :silent !firefox In the above, there is no space after ! and the Vim command has the form :!start program file which causes Vim (on Windows) to run the specified program with the file as an argument. If the program is not in a PATH directory, the full path to the program must be specified. If the program path contains spaces, it is necessary to put the path in quotes ("..."). ---- If using or does not suit a particular requirement, it is possible to use a pattern to select what is needed. For example, the following takes the first match on the current line which starts with 'http' and continues with non-whitespace characters (it finishes at the first space or tab, or at the end of the line): nnoremap :execute 'silent ! start '.matchstr(getline('.'), 'http\S*') ---- On Windows 7 this opens the current file in Chrome (fix the path by replacing "MyName" with what it should be on your system). nnoremap :updatesilent !start "C:\Users\MyName\AppData\Local\Google\Chrome\Application\chrome.exe" "file://%:p" Or it might be here: nnoremap :updatesilent !start "C:\Program Files (x86)\Google\Chrome\Application\chrome.exe" "file://%:p" ---- On Linux, use xdg-open to open the file using its default application. The first of the following mappings saves the current file, then opens it; the second opens the file under the cursor: nnoremap :updatesilent !xdg-open %:p & nnoremap :silent !xdg-open & From 587 Preview current file in Mozilla through localhost When editing an HTML file on Windows, enter the following to save the current file and open it in your default browser: :update|silent ! start %:p On Windows, the current file can be opened in its associated application using the start command of the cmd.exe shell. With this mapping, press F5 to save the current file and open it in its associated application: nnoremap :updatesilent ! start %:p The space after ! is required (on Windows, a command of the form !start xxx causes Vim to run xxx asynchronously; Vim does not run start, whereas ! start xxx invokes the shell cmd.exe to run its start internal command operating on xxx). In the unlikely case that the associated application runs in a console (command prompt window), it would be better to add the /b option so the application runs in the same console as start. That is, change the command to start /b %:p. It is convenient to leave the browser window open: switch back to Vim to do any further editing required, save the file, then switch to the browser and tell it to reload the file with a key like Ctrl-R or F5. A Linux version of the above mapping would use a program like xdg-open: nnoremap :updatesilent !xdg-open %:p & nnoremap :silent updatesilent !xdg-open %:p & From 684 Preview current HTML in browser on Mac OS X *''TODO'': Clean following. There are a few tips on previewing current HTML documents in a Windows browser, but none I could find for Mac OS X. By studying the others, though, I stumbled on a mapping that works. The at the end anticipates the "Hit ENTER or type command to continue" message. nnoremap :!open -a Safari % ;Comments To expand on this: general -- "open x" will open "x" with the default application ... "open -a applicationName x" will open "x" with application "applicationName" general -- "" creates a shortcut using the Command (Apple) key improved(?) -- "nnoremap f :exe ':silent !open -a /Applications/Path/To/Firefox.app %'" (prevents remapping, runs quietly, shortcut is now Command+p,f for "preview, firefox" ... helpful if you preview in multiple browsers) improved(?) -- "nnoremap p :exe ':silent !open %'" (same, but creates a generic "preview" using default applications ... helpful if you only preview in that browser, or can use it on other file types) other (open directory in Finder) -- "nnoremap d :exe ':silent !open -a finder %:p:h'" (%:p:h completely expands the file path and removes the file name so only directory is left) other (apply same to netrw browser, ie, :Ex command) -- add to .vimrc/.gvimrc: "let g:netrw_browsex_viewer = 'open'" (this is not necessary starting with 7.2, netrw now uses "open" as the default for "x"). (when using netrw browser, hit "x" while cursor is on file name to open that file with the default application ... if the cursor is over a directory, it will open in Finder) Also, for Mac users, here is a way to save the current PHP file then preview it through localhost (TODO see %:p:s?pat?sub? below for Vim procedure): nnoremap :update!open -a Google\ Chrome `echo http://localhost/${PWD\#*/*/*/*/*/}/%` From 1015 Preview file on localhost Using this mapping will save the current file (if changed), then preview it on localhost. For example, if you are editing file /var/www/html/one/two/my.html, pressing F5 will use Firefox to open http://localhost/one/two/my.html. nnoremap :silent updatesilent !firefox %:p:s?\(.\{-}/\)\{4}?http://localhost/? The core command has the form !firefox %:p:s?pat?sub? which uses the shell to start Firefox to open the current file (%) with its name modified by forming the full path (:p) then substituting the pattern pat with sub. The pattern is \(.\{-}/\)\{4} which finds exactly four occurrences of any character (.) repeated as few times as possible (\{-}) up to and including the next slash (/). If the file being edited is my.html in directory /var/www/html/one/two, the pattern matches /var/www/html/ (the first occurrence of four slashes, with any characters between). From 201110 Preview current HTML in browser on Linux This mapping allows you to use a browser to preview the HTML file currently being edited. nnoremap :silent updatesilent !firefox %:p & If necessary, the current file is saved, then Firefox is used to open the file. Replace "firefox" with the name of another browser if wanted, such as "chromium-browser". Open an HTML file in a browser :TODO: Put a link somewhere in this tip to the result of merging all the stuff on opening HTML files. See also Open web browser to display URL under cursor *306 Open a web-browser with the URL in the current line *555 Vim as bookmark manager *732 Quick launch html and other Windows documents *1549 Execute external programs asynchronously under Windows launch browser or document in associated app *1656 Automatically refresh display of html on saving file uses MozRepl addon for Firefox Display information for word under cursor in a web browser *394 Internet search for the current word *598 PHP online help *896 Lookup the city and state of a given US Zip code using TCL *922 Launch lynx to get info for the current word *926 QT Help from Vim *933 Search the web for text selected in Vim *1200 View PHP documentation for current word *1354 Online documentation for word under cursor *1377 Context sensitive access to MSDN help Configure so "view source" opens in Vim *118 Use gvim to view page source in Internet Explorer *134 View Source in IE6 using VIM *581 Using vim to view source and edit textarea in mozilla/firebird *805 Use gvim as an external editor for Windows apps *1156 View Source from Internet Explorer in gvim Other *317 Vim key bindings for Firefox *691 Use gf to open a file via its URL *305 Best Vim Tips has a launching section *1440 Launch files in new tabs under Windows *1552 Launch files in new tabs under Unix *1652 Launch remote batch jobs and view results in Vim *299 Open file under cursor *485 Open a window with the man page for the word under the cursor *506 Open Windows Help files on a specific topic *557 Opening several files in vim via ListFile *1356 Open PDF files Comments I have removed the "Related plugins" section as the only plugin listed is obsolete and relies on Perl to do what can be done in plain Vim. JohnBeckett 10:16, April 18, 2012 (UTC) ---- I am working on merging a lot of tips related to opening HTML files in browsers, and documents in associated applications. I don't think anything more will be merged in to here. I will soon clean all this up and see how large the result is (some of it might then be moved elsewhere). JohnBeckett 11:21, April 26, 2012 (UTC) ---- In mappings, I am using F5 to open the current file and F8 to open the file under the cursor. Might rethink that after all merging complete. JohnBeckett 23:56, April 26, 2012 (UTC) ---- TODO: Probably need shellescape() or at least quotes in some of the mappings to handle embedded spaces. I think the Linux mappings also need & appended. JohnBeckett 05:01, April 27, 2012 (UTC)